import { getUserAPI } from '@/apis/User';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useCartStore } from './cartStore';
import { mergeCartAPI } from '@/apis/cart';
export const useUserStore = defineStore('user', () => {
  const userInfo = ref({})
  const cartStore = useCartStore()
  const getUserInfo = async ({ account, password }) => {
    userInfo.value = await getUserAPI({ account, password })
    //合并购物车
    await mergeCartAPI(cartStore.cartList.map(item => {
      return {
        skuId: item.skuId,
        selected: item.selected,
        count: item.count
      }
    }))
    cartStore.updateCart()
  }
  const clearUserInfo = () => {
    userInfo.value = {}
    cartStore.clearCart()
  }
  return {
    userInfo,
    getUserInfo,
    clearUserInfo
  }
},
  {
    persist: true,
  }
)
